/****************************************************
 * 创建人: @author zoujian    
 * 创建时间: 2021-6-22/11:45
 * 项目名称: design_pattern
 * 文件名称: ReentrantLockTest2.java
 * 文件描述: @Description: 可重入锁
 *
 * All rights Reserved, Designed By 投资交易团队
 * @Copyright:2016-2021
 *
 ********************************************************/
package com.zj.designpattern.lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 包名称：com.zj.designpattern.lock
 * 类名称：ReentrantLockTest2
 * 类描述：可重入锁
 * 创建人：@author zoujian
 * 创建时间：2021-6-22/11:45
 */
public class ReentrantLockTest2 {
    private static final Lock lock = new ReentrantLock();

    public static void main(String[] args) {
        new Thread(()->test(),"线程A").start();
        new Thread(()->test(),"线程B").start();
        new Thread(()->test(),"线程C").start();
        new Thread(()->test(),"线程D").start();
        new Thread(()->test(),"线程E").start();

    }

    public static void test() {
        for (int i = 0; i < 2; i++) {
            try {
                lock.lock();
                System.out.println(Thread.currentThread().getName() + "获取了锁");
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        }
    }

}
